﻿using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp54.Models;

namespace WpfApp54
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        //在WPF中，如果数据源（List类型）发生变换话时，是不会自动实现数据增加和修改的，必须要使用ObservableCollection
        ObservableCollection<Product> products;
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<Product> list = new List<Product>();
            for (int i = 0; i < 20; i++)
            {
                list.Add(new Product()
                {
                    Number = "Number:" + i,
                    Name = "Product " + i,
                    Cost = i,
                    Description = "Description " + i
                });
            }

            products = new ObservableCollection<Product>(list);

            ListBox.ItemsSource = products;
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender is ListBox listBox)
            {
                if (listBox.SelectedItem is Product product)
                {
                    TbNumber.Text = product.Number;
                }
            }
        }

        private void BtnModify_Click(object sender, RoutedEventArgs e)
        {
            products.Add(new Product()
            {
                Number = "Number : " + 30,
                Name = "Product " + 30,
                Cost = 30,
                Description = "Description " + 30
            });
        }
       
    }
}